package com.guo.worktool.manager.factory.StrConvertFactory;

import com.guo.worktool.common.comps.RedisUtil;
import com.guo.worktool.common.enums.EnumSQLType;
import com.guo.worktool.common.model.RequestWorkToolBody;
import com.guo.worktool.manager.AsyncManager;
import com.guo.worktool.manager.factory.AsyncFactory;
import com.guo.worktool.utils.ServletUtils;
import com.guo.worktool.utils.ip.IpUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

@Component
public class ConvertFactory {

    private Map<String, StrConvertService> convertServiceMap = new HashMap<>();

    @Autowired
    private void setConvertServiceMap(List<StrConvertService> strConvertServiceList) {
        if (!CollectionUtils.isEmpty(strConvertServiceList)) {
            convertServiceMap = strConvertServiceList.stream().collect(Collectors.toMap(
                    item -> Objects.requireNonNull(
                                    AnnotationUtils.findAnnotation(item.getClass(), Service.class)
                            )
                            .value()
                    , v -> v
                    , (x, y) -> x
            ));
        }
    }

    public StrConvertService findConvert(RequestWorkToolBody workToolBody) {
        EnumSQLType enumSQLType = EnumSQLType.fromCode(workToolBody.getType());
        if (ObjectUtils.notEqual(EnumSQLType.NULL, enumSQLType)) {
            return convertServiceMap.get(enumSQLType.getKey());
        }
        throw new RuntimeException("没有找到可用实例");
    }
}
